import { Component, OnInit } from '@angular/core';
import {AppletSettingService} from "../applet-setting.service";
import {NzMessageService} from 'ng-zorro-antd';
import {ActivatedRoute, Router} from '@angular/router';
import {LocalStorageService} from '../../../lib/service/local-storage.service';
import {FormBuilder, FormGroup} from "@angular/forms";

@Component({
  selector: 'app-setting',
  templateUrl: './setting.component.html',
  styleUrls: ['./setting.component.scss'],
  providers: [AppletSettingService]
})
export class SettingComponent implements OnInit {
  validateForm: FormGroup;

  applet_id;
  applet_uuid;
  pay_key;
  mch_id;
  apiclient_cert;
  apiclient_key;
  AppID;
  appletName;

  isVisible = false;
  isVisible2 = false;

  constructor(private fb: FormBuilder,
              private appletSettingService: AppletSettingService,
              private _message: NzMessageService,
              private router: Router,
              private route: ActivatedRoute,
              private localStorageService: LocalStorageService,
              ) { }

  ngOnInit() {
    this.validateForm = this.fb.group({
      applet_id: [ null ],
      appletName: [ null ],
      mch_id: [ null ],
      pay_key: [ null ],
      apiclient_cert: [ null ],
      apiclient_key: [ null ],
    });

    const applet = this.localStorageService.getNowApplet();
    this.applet_id = applet.id;
    this.validateForm.controls['applet_id'].setValue(this.applet_id);
    this.applet_uuid = applet.applet_uuid;
    this.appletName = applet.nick_name;
    if (!this.appletName) {
      this.getNowApplet();
    } else {
      this.validateForm.controls['appletName'].setValue(this.appletName);
    }

    this.getPayment();
  }

  // 获取支付信息
  getPayment() {
    this.appletSettingService.getPayment().subscribe(res => {
      if (res.code === 2000) {
        if (!res.data || !res.data.AppID) {
          this.isVisible2 = true;
        } else {
          this.pay_key = res.data.pay_key;
          this.AppID = res.data.AppID;
          this.mch_id = res.data.mch_id;
          this.apiclient_cert = res.data.apiclient_cert;
          this.apiclient_key = res.data.apiclient_key;

          this.validateForm.controls['mch_id'].setValue(this.mch_id);
          this.validateForm.controls['pay_key'].setValue(this.pay_key);
          this.validateForm.controls['apiclient_cert'].setValue(this.apiclient_cert);
          this.validateForm.controls['apiclient_key'].setValue(this.apiclient_key);
        }
      } else {
        this._message.error(res.message);
      }
    });
  }

  // 获取小程序信息
  getNowApplet() {
    this.appletSettingService.getNowApplet().subscribe(res => {
      this.appletName = res.data.nick_name;
      this.validateForm.controls['appletName'].setValue(this.appletName);
    });
  }

  // 绑定支付系统
  _submitForm() {
    this.isVisible = true;
  }

  bindPaySubmit() {
    this.isVisible = false;
    this.appletSettingService.bindPaySubmit(this.validateForm.value).subscribe(
      res => {
        if (res.code === 2000) {
          this._message.success('保存成功');
        } else {
          this._message.error(res.message);
        }
      }
    );
  }
}
